package com.fas.system.service.impl;

import com.fas.common.utils.DictUtils;
import com.fas.system.domain.FasEquipmentAlarm;
import com.fas.system.domain.FasWordOrder;
import com.fas.system.domain.vo.EquipmentAlarmVo;
import com.fas.system.domain.vo.WordOrderAndEquipment;
import com.fas.system.mapper.*;
import com.fas.system.service.IFasEquipmentAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class FasEquipmentAlarmServiceImpl implements IFasEquipmentAlarmService {

    protected final Logger logger = LoggerFactory.getLogger(FasEquipmentAlarmServiceImpl.class);

    @Resource
    private FasEquipmentAlarmMapper fasEquipmentAlarmMapper;
    @Resource
    private FasEquipmentMapper fasEquipmentMapper;
    @Resource
    private FasWordOrderMapper fasWordOrderMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private FasBuildingCategoryMapper fasBuildingCategoryMapper;
    @Resource
    private FasCooperationTradeMapper fasCooperationTradeMapper;

    @Override
    public int insertFasEquipmentAlarm(FasEquipmentAlarm fasEquipmentAlarm) {
        return fasEquipmentAlarmMapper.insertFasEquipmentAlarm(fasEquipmentAlarm);
    }

    @Override
    public int updateFasEquipmentAlarm(FasEquipmentAlarm fasEquipmentAlarm) {
        return fasEquipmentAlarmMapper.updateFasEquipmentAlarm(fasEquipmentAlarm);
    }

    @Override
    public List<FasEquipmentAlarm> findAllRecord(FasEquipmentAlarm fasEquipmentAlarm) {
        return fasEquipmentAlarmMapper.findAllRecord(fasEquipmentAlarm);

    }

    @Override
    public int appointIpqc(FasEquipmentAlarm fasEquipmentAlarm) {
        return fasEquipmentAlarmMapper.batchUpdateAlarm(fasEquipmentAlarm);
    }

    @Override
    public EquipmentAlarmVo detailInfo(String equipmentNumber) {
        List<EquipmentAlarmVo> equipmentAlarmVos = fasEquipmentMapper.selectHomePageById(equipmentNumber, null);
        if (equipmentAlarmVos.size() > 0) {
            equipmentAlarmVos.get(0);
        }
        return null;
    }

    @Override
    public List<WordOrderAndEquipment> inspectionList(Integer deptId) {
        List<WordOrderAndEquipment> list = fasEquipmentAlarmMapper.inspectionList(deptId);
        list.forEach(e -> e.setSystemName(DictUtils.getDictLabel("sys_equipment_system", e.getSystemId())));
        return list;
    }

    @Transactional
    @Override
    public int submit(FasEquipmentAlarm fasEquipmentAlarm) {
        if (fasEquipmentAlarm.getTreatmentMethod() != null && fasEquipmentAlarm.getTreatmentMethod() == 1) {
            //生成工单表
            FasWordOrder fasWordOrder = new FasWordOrder();
            fasWordOrderMapper.insertNullData(fasWordOrder);
            fasEquipmentAlarm.setWordOrderId(fasWordOrder.getId());
        }
        fasEquipmentAlarm.setIpqcSubmitDate(new Date());
        return fasEquipmentAlarmMapper.updateFasEquipmentAlarm(fasEquipmentAlarm);
    }

    @Override
    public FasEquipmentAlarm findById(Integer id) {
        return fasEquipmentAlarmMapper.findById(id);
    }

    public void syncSendMsg(Integer alarmId) {
        logger.info("同步发送短信给维保单位人员");
        FasEquipmentAlarm fasEquipmentAlarm = fasEquipmentAlarmMapper.findById(alarmId);
        // 当前报警记录没有选择巡检员的
        if (!Objects.isNull(fasEquipmentAlarm) && fasEquipmentAlarm.getIpqcUserId() == null) {
            Integer equipmentId = fasEquipmentAlarm.getEquipmentId();
            // 自动选择一名巡检员接受该工单
            List<Integer> ipqcList = sysUserMapper.selectEquipmentUserById(equipmentId);
            if (!CollectionUtils.isEmpty(ipqcList)) {
                // 随机指定一名维保人员
//                int random = (int)(Math.random()*ipqcList.size());
//                Integer userId = ipqcList.get(random);
                Integer userId = ipqcList.get(0);
                FasEquipmentAlarm saveVo = new FasEquipmentAlarm();
                saveVo.setIpqcUserId(userId);
                saveVo.setAlarmAckUser(-1); // userId为-1 则表示为系统自动分配
                HashMap<String, Object> map = new HashMap<>();
                map.put("ids", Collections.singleton(alarmId));
                saveVo.setParams(map);
                fasEquipmentAlarmMapper.batchUpdateAlarm(saveVo);
                logger.info("系统已将该报警信息分配给，维保单位管理员编号为：" + userId);
                return;
            }
            logger.debug("设备编号为: " + equipmentId + " 所属的单位下面没有维保单位管理员");
        }
        logger.debug("该报警信息已被处理，报警编号为：" + alarmId);
    }

}
